package com.taichuan.selfcheck.ad;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.util.Log;
import android.view.SurfaceHolder;

import com.taichuan.view.R;

import java.io.File;
import java.io.FileInputStream;

/**
 * Created by OISNY on 2016/7/15.
 */
public class VideoInterface implements OnCompletionListener, OnErrorListener {
    private OnMediaPlayerListener mOnMediaPlayerListener;
    private OnSurfaceViewListener mSurfaceViewSizedListener;
    private MediaPlayer mMediaPlayer;
    private PlayAttrs mPlayAttrs;
    private SurfaceHolder mHolder;
    private Context mContext;

    public VideoInterface(Context context) {
        mContext = context;
    }

    public void setOnMediaPlayerListener(OnMediaPlayerListener listener) {
        mOnMediaPlayerListener = listener;
    }

    public void setOnSurfaceViewListener(OnSurfaceViewListener listener) {
        mSurfaceViewSizedListener = listener;
    }

    private void createPlayVideo() {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setDisplay(mHolder);
    }

    public void play(SurfaceHolder holder, PlayAttrs playAttrs) {
        mHolder = holder;
        mPlayAttrs = playAttrs;
        if (mMediaPlayer == null) {
            createPlayVideo();
        }
        playVideo();
    }

    public void play(PlayAttrs playAttrs) {
        mPlayAttrs = playAttrs;
        if (mMediaPlayer == null) {
            Log.e("VideoTestInterface", "MeidiaPlayer == null");
//            throw new RuntimeException("MediaPlayer is null !");
//            createPlayVideo();
        }

        playVideo();
    }

    private void playVideo() {
        try {
            Log.e(getClass().getSimpleName(), " #[ videoPlay ]# : video play path : "
                    + mPlayAttrs.playPath);
            mMediaPlayer.reset();
            try {
                mMediaPlayer.setDataSource(new FileInputStream(new File(
                        mPlayAttrs.playPath)).getFD());
                mMediaPlayer.prepare();
            } catch (Exception e) {
                e.printStackTrace();
                AssetFileDescriptor assetFileDescriptor = mContext.getResources()
                        .openRawResourceFd(R.raw.video_error);
                mMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(),
                        assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
                mMediaPlayer.prepare();
            }
            if (mPlayAttrs.playProgress > 0) {
                mMediaPlayer.seekTo(mPlayAttrs.playProgress);
            }
            if (mSurfaceViewSizedListener != null) {
                mSurfaceViewSizedListener.onSurfaceViewChanged(
                        mMediaPlayer.getVideoWidth(),
                        mMediaPlayer.getVideoHeight());
            }
            mMediaPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
            release();
            onCompletion(mMediaPlayer);
        }
    }

    public void release() {
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying()) {
                mPlayAttrs.playProgress = mMediaPlayer
                        .getCurrentPosition();
                mMediaPlayer.stop();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (mMediaPlayer != null) {
                mMediaPlayer.release();
            }
            mMediaPlayer = null;
        }
    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        Log.e(getClass().getSimpleName(), " #[ videoPlay ]# : onCompletion!");
        mPlayAttrs.playProgress = 0;
        mPlayAttrs.playCompletion = true;
        if (mOnMediaPlayerListener != null) {
            mOnMediaPlayerListener.onCompletion(arg0, mPlayAttrs);
        }
    }

    @Override
    public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
        Log.e(getClass().getSimpleName(), " #[ videoPlay ]# : onError!");
        mPlayAttrs.playProgress = 0;
        mPlayAttrs.playError = true;
        if (mOnMediaPlayerListener != null) {
            mOnMediaPlayerListener.onError(arg0, mPlayAttrs);
        }
        return false;
    }

    public interface OnMediaPlayerListener {
        void onCompletion(MediaPlayer arg0, PlayAttrs attrs);

        void onError(MediaPlayer arg0, PlayAttrs attrs);
    }

    public interface OnSurfaceViewListener {
        void onSurfaceViewChanged(int width, int height);
    }

    public static class PlayAttrs {
        private int playPosition = 0;//当前播放
        private String playPath;//当前播放路径
        private int playProgress = 0;//当前播放进度
        private boolean playFix;//是否启动自适应
        private boolean playCompletion = true;//是否播放完成
        private boolean playError = false;//是否播放出错

        public PlayAttrs(String playPath, boolean playFix) {
            this.playPath = playPath;
            this.playFix = playFix;
        }

        public int getPlayPosition() {
            return playPosition;
        }

        public void setPlayPosition(int playPosition) {
            this.playPosition = playPosition;
        }

        public String getPlayPath() {
            return playPath;
        }

        public void setPlayPath(String playPath) {
            this.playPath = playPath;
        }

        public int getPlayProgress() {
            return playProgress;
        }

        public void setPlayProgress(int playProgress) {
            this.playProgress = playProgress;
        }

        public boolean isPlayFix() {
            return playFix;
        }

        public void setPlayFix(boolean playFix) {
            this.playFix = playFix;
        }

        public boolean isPlayCompletion() {
            return playCompletion;
        }

        public void setPlayCompletion(boolean playCompletion) {
            this.playCompletion = playCompletion;
        }

        public boolean isPlayError() {
            return playError;
        }

        public void setPlayError(boolean playError) {
            this.playError = playError;
        }

        @Override
        public String toString() {
            return "PlayAttrs{" +
                    "playPosition=" + playPosition +
                    ", playPath='" + playPath + '\'' +
                    ", playProgress=" + playProgress +
                    ", playFix=" + playFix +
                    ", playCompletion=" + playCompletion +
                    ", playError=" + playError +
                    '}';
        }
    }
}
